package lxt.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.entity.DriverInfo;
import lxt.entity.Parameter;
import lxt.service.DriverService;
import lxt.service.ParameterService;
/**
 * 驾驶员管理action
 * @author Lit
 *
 */
public class DriverAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	//定义属性
	private String driverId;
    private String name;
    private String phone;
    private String driverLicense;
    private String licenseType;
    private String licenseYear;
    private String note;
    //定义service
    private DriverService driverService;
    private ParameterService parameterService ;
    //定义对象
    private List<Parameter> paramter ;
	private List<DriverInfo> drivers;
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
	
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			System.out.println("对象 "+drivers);
			//获得驾驶员列表
			drivers = driverService.loadDriver(name, driverLicense, offset, pageCounts);
			//驾龄计算
			if(drivers.size()>0){
				//日期
				Calendar ca = Calendar.getInstance();
				ca.setTime(new Date());
				int TodayY = ca.get(Calendar.YEAR);
				for(int i=0;i<drivers.size();i++){
					String DriverY = drivers.get(i).getLicenseYear();
					if(DriverY!=null&&!DriverY.equals("")){
						int year =TodayY - Integer.parseInt(DriverY);
						drivers.get(i).setLicenseYear(String.valueOf(year));
					}
				}
			}
			//获取总的记录数
			totalCounts = driverService.loadAllDriverCount(name, driverLicense);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			paramter = parameterService.loadByParamType("licenseType");
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 */
	public String add(){
		try{
			//定义变量
			DriverInfo driver ;
			List<DriverInfo> list = this.driverService.loadDriverByName(name);
			if(list.size()!=0){
				this.addActionMessage("该驾驶员已经存在，请重新填写");
				paramter = parameterService.loadByParamType("licenseType");
				return INPUT;
			}
			else{
				driver = new DriverInfo();
				driver.setDriverId(driverId);
				driver.setDriverLicense(driverLicense);
				//driver.setDriverWorkloads(driverWorkloads);
				driver.setLicenseType(licenseType);
				driver.setLicenseYear(licenseYear);
				driver.setName(name);
				driver.setNote(note);
				driver.setPhone(phone);
				this.driverService.addDriver(driver);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			paramter = parameterService.loadByParamType("licenseType");
			//获得参数
			DriverInfo driver = this.driverService.loadDriverById(driverId);
			//为页面属性赋值
			this.driverId = driver.getDriverId();
			this.driverLicense = driver.getDriverLicense();
			this.licenseType = driver.getLicenseType();
			this.licenseYear = driver.getLicenseYear();
			this.name = driver.getName();
			this.note = driver.getNote();
			this.phone = driver.getPhone();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			DriverInfo driver = this.driverService.loadDriverById(driverId);
			//更新目标对象的目标修改属性(部分属性)
			driver.setDriverLicense(driverLicense);
			//driver.setDriverWorkloads(driverWorkloads);
			driver.setLicenseType(licenseType);
			driver.setLicenseYear(licenseYear);
			driver.setName(name);
			driver.setNote(note);
			driver.setPhone(phone);
			driverService.updateDriver(driver);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			driverService.deleteDriver(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	
	/**
	 * get set
	 */
	
	public List<DriverInfo> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<DriverInfo> drivers) {
		this.drivers = drivers;
	}
	public String getDriverId() {
		return driverId;
	}
	public void setDriverId(String driverId) {
		this.driverId = driverId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getDriverLicense() {
		return driverLicense;
	}
	public void setDriverLicense(String driverLicense) {
		this.driverLicense = driverLicense;
	}
	public String getLicenseType() {
		return licenseType;
	}
	public void setLicenseType(String licenseType) {
		this.licenseType = licenseType;
	}
	public String getLicenseYear() {
		return licenseYear;
	}
	public void setLicenseYear(String licenseYear) {
		this.licenseYear = licenseYear;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public DriverService getDriverService() {
		return driverService;
	}
	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	public List<Parameter> getParamter() {
		return paramter;
	}
	public void setParamter(List<Parameter> paramter) {
		this.paramter = paramter;
	}
	public ParameterService getParameterService() {
		return parameterService;
	}
	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}
	
}
